package com.sbw.tinysoft.Utils;

import com.sbw.tinysoft.Utils.analyzer.impl.CsdnWeeklyDocumentAnalyzer;
import com.sbw.tinysoft.Utils.analyzer.impl.VisaIpMethodListDocumentAnalyzer;
import com.sbw.tinysoft.Utils.analyzer.impl.VisaMethodDayListDocumentAnalyzer;
import com.sbw.tinysoft.Utils.analyzer.impl.VisaServiceDayListDocumentAnalyzer;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章抓取工具
 * Created by sbw on 2017/3/11.
 */
public class ArticleSpider {

    private static final Logger log = LoggerFactory.getLogger(ArticleSpider.class);


    public static <T> List<T> forEntityList(String url, CsdnWeeklyDocumentAnalyzer docAnalyzer, Class<T> type) throws Exception {

        log.info("开始抓取CDSN文章："+url);

        List<T> results = new ArrayList<>();
        docAnalyzer.forListMap(Jsoup.connect(url).timeout(50000).get()).forEach(map->{
            try {
                results.add(TinyUtil.mapToBean(map, type));
            } catch (Exception ignored) {}
         });
        return results;
    }

    public static <T> List<T> forVisaApi2ServicesList(String url, VisaServiceDayListDocumentAnalyzer docAnalyzer, Class<T> type) throws Exception {

        log.info("开始抓取 Services 列表 ："+url);

        List<T> results = new ArrayList<>();
        docAnalyzer.forListMap(Jsoup.connect(url).userAgent(TinyUtil.user_agent).timeout(50000).get()).forEach(map->{
            try {
                results.add(TinyUtil.mapToBean(map, type));
            } catch (Exception ignored) {}
        });
        return results;
    }

    public static <T> List<T> forService2MethodList(String url, VisaMethodDayListDocumentAnalyzer docAnalyzer, Class<T> type) throws Exception {

        log.info("开始抓取 接口方法 列表："+url);

        List<T> results = new ArrayList<>();
        docAnalyzer.forListMap(Jsoup.connect(url).userAgent(TinyUtil.user_agent).timeout(50000).get()).forEach(map->{
            try {
                results.add(TinyUtil.mapToBean(map, type));
            } catch (Exception ignored) {}
        });
        return results;
    }

    public static <T> List<T> forMethod2IpsList(String url, VisaIpMethodListDocumentAnalyzer docAnalyzer, Class<T> type) throws Exception {

        log.info("开始抓取 方法 被调用服务器地址 ："+url);

        List<T> results = new ArrayList<>();
        docAnalyzer.forListMap(Jsoup.connect(url).userAgent(TinyUtil.user_agent).timeout(50000).get()).forEach(map->{
            try {
                results.add(TinyUtil.mapToBean(map, type));
            } catch (Exception ignored) {}
        });
        return results;
    }
}